﻿using System.Collections.Generic;
using UploadClient.Interfaces.Plugins;
using Microsoft.Win32;

namespace AutostartPlugin
{
    /// <summary>
    /// Starts the program on systemstart.
    /// </summary>
    public class Autostart : IPlugin
    {
        const string RUN_LOCATION = @"Software\Microsoft\Windows\CurrentVersion\Run";
        string assemblyLocation;
        Configurations config;
        IHost host;
        string keyName;

        #region IPlugin Members

        public void Init(IHost host)
        {
            this.host = host;
            config = new Configurations(host);
            keyName = host.GetProgramName();
            assemblyLocation = host.GetExecutingAssemblyLocation();
        }

        public void Init(IHost host, IPluginConfig settings)
        {
            Init(host);
            config = new Configurations(host, settings.Settings);

            if (settings.PluginEnabled) EnablePlugin();
        }

        public IEnumerable<IPluginConfig> GetConfiguration()
        {
            return new IPluginConfig[] { config };
        }

        public void EnablePlugin()
        {
            config.PluginEnabled = true;
            if (!IsAutoStartEnabled(keyName, assemblyLocation)) SetAutoStart(keyName, assemblyLocation);
        }

        public void DisablePlugin()
        {
            config.PluginEnabled = false;
            if (IsAutoStartEnabled(keyName, assemblyLocation)) UnSetAutoStart(keyName);
        }

        #endregion

        /// <summary>
        /// Sets the autostart value for the assembly.
        /// </summary>
        /// <param name="keyName">Registry Key Name</param>
        /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
        void SetAutoStart(string keyName, string assemblyLocation)
        {
            string assLoc = assemblyLocation;

            if (config.Settings["startMinimized"].ToString().Equals("True"))
                assLoc += " -t";

            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
            key.SetValue(keyName, assLoc);
        }

        /// <summary>
        /// Returns whether auto start is enabled.
        /// </summary>
        /// <param name="keyName">Registry Key Name</param>
        /// <param name="assemblyLocation">Assembly location (e.g. Assembly.GetExecutingAssembly().Location)</param>
        bool IsAutoStartEnabled(string keyName, string assemblyLocation)
        {
            RegistryKey key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION);
            if (key == null)
                return false;

            string value = (string)key.GetValue(keyName);
            if (value == null)
                return false;

            return (value.Equals(assemblyLocation) || value.Equals(assemblyLocation + " -t"));
        }

        /// <summary>
        /// Unsets the autostart value for the assembly.
        /// </summary>
        /// <param name="keyName">Registry Key Name</param>
        void UnSetAutoStart(string keyName)
        {
            RegistryKey key = Registry.CurrentUser.CreateSubKey(RUN_LOCATION);
            key.DeleteValue(keyName);
        }
    }
}
